﻿using System.Collections.Generic;
using System.Linq;

namespace Basmus.Values
{
    public static class SetEqualExtensions
    {
        public static IEnumerable<T> RemoveDefault<T>(this IEnumerable<T> elements, IEnumerable<T> defaultElements, SetEqualType setEqualType, IEqualityComparer<T> comparer)
        {
            if (setEqualType == SetEqualType.SetEqual && !elements.Except(defaultElements, comparer).Any())
            {
                return Enumerable.Empty<T>();
            }
            if (setEqualType == SetEqualType.OrderSetEqual && elements.SequenceEqual(defaultElements, comparer))
            {
                return Enumerable.Empty<T>();
            }
            return elements;
        }

        public static IEnumerable<T> RemoveDefault<T>(this IEnumerable<T> elements, IEnumerable<T> defaultElements, SetEqualType setEqualType)
        {
            return RemoveDefault(elements, defaultElements, setEqualType, EqualityComparer<T>.Default);
        }

        public static IEnumerable<T> AddDefault<T>(this IEnumerable<T> elements, IEnumerable<T> defaultElements, SetEqualType setEqualType, IEqualityComparer<T> comparer)
        {
            if (setEqualType == SetEqualType.SetEqual && !elements.Any())
            {
                return defaultElements;
            }
            if (setEqualType == SetEqualType.OrderSetEqual && !elements.Any())
            {
                return defaultElements;
            }
            return elements;
        }

        public static IEnumerable<T> AddDefault<T>(this IEnumerable<T> elements, IEnumerable<T> defaultElements, SetEqualType setEqualType)
        {
            return AddDefault(elements, defaultElements, setEqualType, EqualityComparer<T>.Default);
        }
    }
}
